import { getUser, getFavorites, getHistory, toggleFavorite as toggleFav } from '../../utils/storage';
import type { Song } from '../../utils/api';
import { playSong, getAudio, getState, pause } from '../../utils/audio';

const defaultAvatar = '';

Component({
  data: {
    user: null as any,
    defaultAvatar,
    favorites: [] as Song[],
    history: [] as Song[],
    currentSong: null as Song | null,
    paused: true
  },
  lifetimes: {
    attached() { 
      this.refresh();
      // 监听音频状态变化
      const a = getAudio();
      a.offTimeUpdate();
      a.onTimeUpdate(() => {
        const state = getState();
        this.setData({ 
          currentSong: state.currentSong,
          paused: state.paused 
        });
      });
    }
  },
  pageLifetimes: {
    show() { this.refresh(); }
  },
  methods: {
    refresh() {
      const user = getUser();
      const favorites = getFavorites();
      const history = getHistory();
      this.setData({ user, favorites, history });
    },
    goLogin() {
      wx.navigateTo({ url: '/pages/login/index' });
    },
    onPlay(e: any) {
      const song: Song = e.detail.song;
      wx.navigateTo({ url: `/pages/player/index?id=${song.id}` });
    },
    onToggleLike(e: any) {
      const song: Song = e.detail.song;
      const nextFav = toggleFav(song);
      const favIds = new Set(nextFav.map(s => s.id));
      const favorites = nextFav;
      const history = this.data.history.map(s => ({ ...s, liked: favIds.has(s.id) }));
      this.setData({ favorites, history });
    },
    
    onSongTap(e: any) {
      const song: Song = e.currentTarget.dataset.song;
      playSong(song, () => this.onNextSong());
      this.setData({ currentSong: song, paused: false });
      // 播放后跳转到播放界面
      wx.navigateTo({ url: `/pages/player/index?id=${song.id}` });
    },
    
    onPlay(e: any) {
      const song: Song = e.currentTarget.dataset.song;
      playSong(song, () => this.onNextSong());
      this.setData({ currentSong: song, paused: false });
      // 播放后跳转到播放界面
      wx.navigateTo({ url: `/pages/player/index?id=${song.id}` });
    },
    
    onNextSong() {
      // 下一首功能实现
      const list = this.data.history.length > 0 ? this.data.history : this.data.favorites;
      const currentSong = this.data.currentSong;
      if (!currentSong || list.length === 0) return;
      
      const currentIndex = list.findIndex(s => s.id === currentSong.id);
      const nextIndex = (currentIndex + 1) % list.length;
      const nextSong = list[nextIndex];
      
      playSong(nextSong, () => this.onNextSong());
      this.setData({ currentSong: nextSong, paused: false });
    },
    
    onFavoritesTap() {
      if (this.data.favorites.length === 0) {
        wx.showToast({ title: '暂无收藏', icon: 'none' });
        return;
      }
      // 跳转到收藏列表页面
      wx.navigateTo({ 
        url: '/pages/search/index?type=favorites&title=我的收藏'
      });
    },
    
    onHistoryTap() {
      if (this.data.history.length === 0) {
        wx.showToast({ title: '暂无历史', icon: 'none' });
        return;
      }
      // 跳转到历史列表页面
      wx.navigateTo({ 
        url: '/pages/search/index?type=history&title=播放历史'
      });
    }
  }
});